#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "data.h"
#include "key.h"

struct skey {
	char * s;
	int tam;
};

int key_maxLen (void) {
	return keyMaxLen;
}

key key_fromString(char * s) {
	key k = (key)calloc(1,sizeof(struct skey));
	if (strlen(s) <= keyMaxLen) {
		if (k != NULL) {
			k->s = (char*)calloc(keyMaxLen+1,sizeof(char));
			k->s = strcpy(k->s,s);
			k->tam = strlen(s);
			}
		}
	return k;
}

void key_toString (key k, char * p) {
	if(k != NULL) {
		if (k->s != NULL) {
			p = strcpy(p,k->s);
			}
	}
}

int key_lenght (key k) {
	if (k != NULL)
		return (k->tam);
	return -1;
}

bool key_eq (key k1, key k2) {
	bool v = (strcmp (k1->s,k2->s) == 0);
	return v;
}

bool key_le (key k1, key k2) {
	int t;
	bool v;
	t = strcmp(k1->s,k2->s);
	if (t < 0) {
		v = TRUE;
	}
	else {
		v = FALSE;
	}
	return v;
}

key key_clone (key k) {
	key clone = key_fromString(k->s);
	return clone;
}

key key_destroy (key k) {
	if (k != NULL) {
		if(k->s != NULL) {
			free(k->s);
			k->s = NULL;
		}
		free(k);
		k = NULL;
	}
	return k;
}
